using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;


using Ultimate.DAL.CollectionClasses;
using Ultimate.DAL;
using Ultimate.DAL.HelperClasses;
using Ultimate.DAL.EntityClasses;
using Ultimate.DAL.FactoryClasses;
using SD.LLBLGen.Pro.ORMSupportClasses;

using System.Web.Security;
using System.Web.Mail;

namespace Ultimate.League
{
	/// <summary>
	/// Summary description for Passwords.
	/// </summary>
	public class Passwords : System.Web.UI.Page
	{
		private void Page_Load(object sender, System.EventArgs e)
		{
			if (Context.User.IsInRole(SecurityLevel.Admin.ToString()))
			{
			}
			else Response.Redirect("~");
		}
		protected void ResetPasswords(object o, System.EventArgs e)
		{
			string _allowedChars = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789!@$?";
			Byte[] randomBytes = new Byte[10];
			char[] chars = new char[10];
			int allowedCharCount = _allowedChars.Length;

			Random randomObj = new Random(DateTime.Now.Millisecond);
			if (Context.User.IsInRole(SecurityLevel.Admin.ToString()))
			{
				int j=0;
				PlayerCollection pc = new PlayerCollection();
				pc.GetMulti(null);
				foreach(PlayerEntity pe in pc.Items)
				{
					if(pe.TestOriginalFieldValueForNull(PlayerFieldIndex.Password))
					{
						for(int i = 0;i<10;i++)
						{
							randomObj.NextBytes(randomBytes);
							chars[i] = _allowedChars[(int)randomBytes[i] % allowedCharCount];
						}
						string password = new string(chars);
						pe.Password=FormsAuthentication.HashPasswordForStoringInConfigFile(password+pe.Email,"md5");
						pe.Save();
						MailMessage mm = new MailMessage();
						mm.To=pe.Email;
						mm.From="Gary@EUPA.ca";
						mm.Subject="Your EUPA password has been reset by an admin";
						mm.Body="Your user data has been imported into the new registration system.\n";
						mm.Body+="The Following Data has been imported\n";
						mm.Body+="Name: "+pe.FirstName+" "+pe.LastName;
						mm.Body+="\nEmail: "+pe.Email;
						mm.Body+="\nAddress: "+pe.Address;	
						mm.Body+="\nCity: "+pe.City;
						mm.Body+="\nProvince: "+pe.Province ;
						mm.Body+="\nPostal Code: "+pe.PostalCode;
						mm.Body+="\nSkill Level (1-9) : "+pe.SkillLevel;
						if (pe.Male==true) mm.Body+="\nGender: Male";
						else mm.Body+="\nGender: Female";
						mm.Body+="\nBirthday: "+pe.Birthdate.ToShortDateString();
						mm.Body+="\nYour EUPA league password has been reset to: "+password+" .\nIf you have any problems please contact your league administrator.";
						
						//SmtpMail.Send(mm);
						j++;
					}
				}
				Response.Write(j.ToString()+" emails sent");

			}
		}
		
		/// <summary>
		/// This can be used to create a random password that can then be emailed to users that forget there passwords
		/// </summary>
		/// <param name="PasswordLength"></param>
		/// <returns></returns>
		public static string CreateRandomPassword(int PasswordLength)
		{
			string _allowedChars = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789!@$?";
			Byte[] randomBytes = new Byte[PasswordLength];
			char[] chars = new char[PasswordLength];
			int allowedCharCount = _allowedChars.Length;

			Random randomObj = new Random(DateTime.Now.Millisecond);

			for(int i = 0;i<PasswordLength;i++)
			{
				randomObj.NextBytes(randomBytes);
				chars[i] = _allowedChars[(int)randomBytes[i] % allowedCharCount];
			}

			return new string(chars);
		}
		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.Load += new System.EventHandler(this.Page_Load);
		}
		#endregion
	}
}
